#include "switchbutton.h"

SwitchButton::SwitchButton(QWidget *parent,int sz_width,int sz_height):QWidget(parent),size_width(sz_width),size_height(sz_height)
{
    resize(size_width,size_height);
}

void SwitchButton::paintEvent(QPaintEvent *)
{
    int r = (int)(size_height*0.4);
    int rd = (int)(size_height*0.5);

    int x1 = (int)(size_width*0.25);
    int y1 = (int)(size_height*0.5);

    int x2 = (int)(size_width*0.75);
    int y2 = (int)(size_height*0.5);

    QPainter p(this);
    p.setPen(Qt::NoPen);
    p.setRenderHint(QPainter::Antialiasing);
    if(!status_isOpen)
    {

        p.setBrush(color_close_background);
        p.drawRoundedRect(rect(),rd,rd);
        p.setBrush(color_close_foregound);
        p.drawEllipse(QPoint(x1,y1),r, r);
    }
    else
    {
        p.setBrush(color_open_background);
        p.drawRoundedRect(rect(),rd,rd);
        p.setBrush(color_open_foregound);
        p.drawEllipse(QPoint(x2,y2),r, r);
    }
}

void SwitchButton::setColor(QColor color1,QColor color2,QColor color3,QColor color4)
{
    color_close_background = color1;
    color_close_foregound = color2;
    color_open_background = color3;
    color_open_foregound = color4;
}

void SwitchButton::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton)
    {
        status_isOpen = !status_isOpen;
        update();
        e->accept();
    }
}
